﻿using Com.Shinian.FcgProtocol.Pb;
using FCG.cache.proxy_model;
using GF.Debug;
using GFFramework.UI;
using Google.Protobuf;
using Network;
using UnityEngine.UI;

[UI((int)WinEnum.Win_Home, "Windows/HomePanel")]
public class CUIHomeWindow : WindowBase
{
    [TransformPath("PVE")] private Button btn_PVE;
    [TransformPath("PVP")] private Button btn_PVP;
    public CUIHomeWindow(string path) : base(path)
    {

    }

    public override void Init()
    {
        base.Init();
        NetMgr.Register(ProxyModelIndex.CardPackPM, onCardPackPMUpdate);
        NetMgr.Register(ProxyModelIndex.Block9_FightScenePM, onBlock9_FightScenePMUpdate);

        btn_PVE.onClick.AddListener(() =>
        {
            findMatch(MatchType.Block9Robot);
        });

        btn_PVP.onClick.AddListener(() =>
        {
            findMatch(MatchType.Block9Pvp);
        });
    }

    public override void Open(WindowData data = null)
    {
        base.Open(data);
    }

    public override void Destroy()
    {
        base.Destroy();
        NetMgr.UnRegister(ProxyModelIndex.PlayerPM, onBlock9_FightScenePMUpdate);
        NetMgr.UnRegister(ProxyModelIndex.PlayerPM, onCardPackPMUpdate);
    }

    private void findMatch(MatchType matchType)
    {
        RequestMatchParam param = new RequestMatchParam
        {
            MatchType = matchType
        };
        byte[] data = new byte[param.CalculateSize()];
        CodedOutputStream stream = new CodedOutputStream(data);
        param.WriteTo(stream);
        WebSocketConnect.instance.Pack((short)FCGChannel.ERequestMatch, data);
    }

    private void onBlock9_FightScenePMUpdate(ProxyModelUpdateEvent bubbleevent)
    {
        UIMgr.OpenScene("FightScene");

        
        Debugger.Log("加载战斗场景");
    }

    void onCardPackPMUpdate(ProxyModelUpdateEvent bubbleevent)
    {
        NetMgr.CloseWait();
    }
}
